/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.sll.service.catalogue.importer.transform;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.Exchange;
import se.sll.service.catalogue.common.property.CatalogProperties;
import se.sll.service.catalogue.common.rest.request.CapacityCreateRequest;
import se.sll.service.catalogue.common.rest.response.VMIdMapResponse;
import se.sll.service.catalogue.importer.extract.HsaIdVmIdProcessor;
import se.sll.service.catalogue.importer.extract.VMCareGiver;
import se.sll.service.catalogue.model.vm.Vantetid;

/**
 *
 * @author khaleddaham
 */
@Slf4j
public class ToCapacity {

    @SuppressWarnings("unchecked")
    public void process(Exchange exchange) throws IOException, FileNotFoundException, CloneNotSupportedException {
        Map map = (Map) exchange.getIn().getBody();
        exchange.getIn().setBody(getCapacityCreateRequestList(
                (List<Vantetid>) map.get("capacity"),
                (HashMap<VMCareGiver, VMIdMapResponse>) map.get("idmap")));
    }

    /**
     * Convert from VM1.0 model to VM2.0 REST model
     *
     * @param waitingTimes
     * @return
     */
    private static Iterable<CapacityCreateRequest> getCapacityCreateRequestList(List<Vantetid> waitingTimes, HashMap<VMCareGiver, VMIdMapResponse> VMToHSAMap) throws FileNotFoundException, IOException, CloneNotSupportedException {
        Set<CapacityCreateRequest> obj = new HashSet<>();
        HsaIdVmIdProcessor p = new HsaIdVmIdProcessor();
        // åtgärdsTyper som skall matcha väntetid 1 och 2
        List<String> reception = Arrays.asList("1");
        // åtgärdsTyper som skall matcha väntetid 3 och 4        
        List<String> examination = Arrays.asList("2");
        // åtgärdsTyper som skall matcha väntetid 5 och 6        
        List<String> operation = Arrays.asList("3", "5");
        // åtgärdstyper som skall mappas om till mottagning.
        List<String> mapToReception = Arrays.asList("4", "6");

        for (Vantetid v : waitingTimes) {
            CapacityCreateRequest req = new CapacityCreateRequest();
            req.setCodeSystem(CatalogProperties.getProperty(CatalogProperties.NAME.CAPACITY_OID));
            VMCareGiver careGiver = new VMCareGiver();
            careGiver.setId(v.getVardgivar().getVardgivarID());
            careGiver.setPartId(v.getVardgivarDel().getVardgivardelID());
            VMIdMapResponse vm = VMToHSAMap.get(careGiver);
            if (vm == null) {
                // Try xls-map
                req.setHsaID(p.getHSAIdForVMId(careGiver));
                if (req.getHsaID() == null) {
                    log.info("No HSA found for {}, {}", careGiver.getId(), careGiver.getPartId());
                    continue;
                }
            } else {
                req.setHsaID(vm.getHsaId());
                if (!VMToHSAMap.get(careGiver).isAllowImport()) {
                    log.info("Import not allowed for {}.", VMToHSAMap.get(careGiver).toString());
                    continue;
                }
            }
            req.setProduct(v.getAtgard().getAtgardID());
            DateFormat dForm = new SimpleDateFormat("yyyyMMddHHmmss");
            try {
                Date date = (Date) dForm.parse(v.getVantetidTomDatum());
                req.setValidTo(date);
            } catch (ParseException ex) {
                log.error(ex.getMessage(), ex);
            }
            if (v.getVtidMottag() != null) {
                CapacityCreateRequest req1 = (CapacityCreateRequest) req.clone();
                req1.setCode("1");
                req1.setValue(v.getVtidMottag());
                if (reception.contains(v.getAtgard().getAtgardTypID())) {
                    obj.add(req1);
                } else {
//                    System.out.println("Will not import: " + req1.toString());
                }
            }
            if (v.getVtidMottagFortur() != null) {
                CapacityCreateRequest req2 = (CapacityCreateRequest) req.clone();
                req2.setCode("2");
                req2.setValue(v.getVtidMottagFortur());
                if (reception.contains(v.getAtgard().getAtgardTypID())) {
                    obj.add(req2);
                } else {
//                    System.out.println("Will not import: " + req2.toString());
                }
            }
            if (v.getVtidOperationMottag() != null) {
                CapacityCreateRequest req3 = (CapacityCreateRequest) req.clone();
                req3.setCode("3");
                req3.setValue(v.getVtidOperationMottag());
                if (!reception.contains(v.getAtgard().getAtgardTypID())) {
                    obj.add(req3);
                } else {
//                    System.out.println("Will not import: " + req3.toString());
                }
            }
            if (v.getVtidOperationMottagFortur() != null) {
                CapacityCreateRequest req3 = (CapacityCreateRequest) req.clone();
                req3.setCode("4");
                req3.setValue(v.getVtidOperationMottagFortur());
                if (!reception.contains(v.getAtgard().getAtgardTypID())) {
                    obj.add(req3);
                } else {
//                    System.out.println("Will not import: " + req3.toString());
                }
            }
            if (v.getVtidOperation() != null) {
                CapacityCreateRequest req4 = (CapacityCreateRequest) req.clone();
                req4.setValue(v.getVtidOperation());
                if (mapToReception.contains(v.getAtgard().getAtgardTypID())) {
                    log.info("re-mapping: " + v.toString());
                    req4.setCode("1");
                    obj.add(req4);
                } else {
                    req4.setCode("5");
                }
                if (!reception.contains(v.getAtgard().getAtgardTypID())) {
                    obj.add(req4);
                } else {
//                    System.out.println("Will not import: " + req4.toString());
                }
            }
            if (v.getVtidOperationFortur() != null) {
                CapacityCreateRequest req4 = (CapacityCreateRequest) req.clone();
                req4.setValue(v.getVtidOperationFortur());
                if (mapToReception.contains(v.getAtgard().getAtgardTypID())) {
                    log.info("re-mapping: " + v.toString());
                    req4.setCode("2");
                    obj.add(req4);
                } else {
                    req4.setCode("6");
                }
                if (!reception.contains(v.getAtgard().getAtgardTypID())) {
                    obj.add(req4);
                } else {
//                    System.out.println("Will not import: " + req4.toString());
                }
            }
        }
        return obj;
    }
}
